<?php
/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/6/30
 * Time: 下午4:54
 */

namespace App\Logic;
use App\Http\Model\Bundle;
use App\Http\Model\GoodsSku;
use App\Http\Model\UserCart AS UserCartModel;
use Exception;
use App\Logic\GoodsSkuLogic;
use App\Logic\BundleLogic;

class UserCartLogic extends BaseLogic
{
    const SKU_TYPE = 'sku';  //商品类型
    const BUNDLE_TYPE = 'bundle';//捆绑商品类型
    protected  $_userCart = null;
    protected  $_user = null;
    protected  $_cartGoods = null;

    public function __construct($userCart)
    {
        $this->_userCart = $userCart;
        return $this;
    }

    public static function initByUserCart($userCart)
    {
        if(!$userCart instanceof UserCartModel)
        {
            throw new Exception('购物车参数错误', -2);
        }

        $re =  new static($userCart);
        if ($userCart->cart_type == self::SKU_TYPE)
        {
            $sku = GoodsSkuLogic::initBySku($userCart->sku_id)->getSku();
        }else{
            $sku = BundleLogic::initByBundle($userCart->bundle_id)->getBundle();
        }
        $re->setCartGoods($sku);
        return $re;
    }

    public static function create($sku, $type, $user)
    {
        $model = new UserCartModel();
        $userCart = new static($model);
        $userCart->setType($type);
        $userCart->setCartGoods($sku);
        $userCart->setUser($user);
        return $userCart;
    }

    private function setCartGoods($sku)
    {
        $this->_cartGoods = $sku;
        if ($sku instanceof GoodsSku)
        {
            $this->_userCart->goods_id = $sku->goods_id;
            $this->_userCart->sku_id = $sku->sku_id;
        }else if($sku instanceof Bundle){
            $this->_userCart->bundle_id = $sku->id;
        }
        return $this;
    }

    private function setType($cart_type)
    {
        $this->_userCart->cart_type = $cart_type;
        return $this;
    }

    public function setNum($num)
    {
        $this->_userCart->count = $num;
        return $this;
    }


    public function setUser($user)
    {
        $this->_userCart->user_id = $user->user_id;
        return $this;
    }

    public function getCartGoods()
    {
        if ($this->_cartGoods == null)
        {
            if ($this->_userCart->cart_type == self::SKU_TYPE)
            {
                $this->_cartGoods = $this->_userCart->sku;
            }else{
                $this->_cartGoods = $this->_userCart->bundle;
            }
        }
        return $this->_cartGoods;
    }

    public function setPrice()
    {
        $this->_userCart->price = $this->_cartGoods->price;
        $this->_userCart->total_price = $this->_cartGoods->price * $this->_userCart->count;
        if ($this->_userCart->cart_type == self::SKU_TYPE)
        {
            $this->_userCart->point = $this->_cartGoods->point;
            $this->_userCart->total_point = $this->_cartGoods->point * $this->_userCart->count;
        }else {
            $bunleGoods = $this->_cartGoods->bundleGoods;
            $point = 0;
            foreach ($bunleGoods as $bgoods)
            {
                $goods = $bgoods->goods;
                $point += $goods->point;
            }
            $this->_userCart->point = $point;
            $this->_userCart->total_point = $point * $this->_userCart->count;
        }
        return $this;
    }

    public function save($data=[])
    {
        $this->_userCart->fill($data)->save();
    }

}